package com.situ.day23;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo {

    @Test
    public void testClass() throws ClassNotFoundException {
        //1.Class.forName(类路径)
        Class clazz1 = Class.forName("com.situ.day23.Student");
        //2.类型.class
        Class clazz2 = Student.class;
        //3.对象.getClass()
        Student student = new Student();
        Class clazz3 = student.getClass();

        System.out.println(clazz1 == clazz2);
        System.out.println(clazz1 == clazz3);
    }

    @Test
    public void testConstructor() {
        Class clazz = Student.class;
        Constructor[] constructors = clazz.getDeclaredConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
            System.out.println(constructor.getName());
            System.out.println(constructor.getModifiers());
        }
    }

    @Test
    public void testConstructor3() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class clazz = Student.class;
        //public Student(Integer id, String name, Integer age, String gender) {
        Constructor constructor = clazz.getConstructor(Integer.class, String.class, Integer.class, String.class);
        //Student student = new Student(1, "zhansgan", 23, "男");
        Student student = (Student) constructor.newInstance(1, "zhansgan", 23, "男");
        System.out.println(student);
    }

    @Test
    public void testMethod() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //Student student = new Student();
        //student.setName("zhangsan");
        Class clazz = Student.class;
        Constructor constructor = clazz.getConstructor();
        Student student = (Student) constructor.newInstance();
        Method method = clazz.getMethod("setName", String.class);
        method.invoke(student, "zhangsan");
        System.out.println(student);
    }

    @Test
    public void test3() throws IllegalAccessException {
        Student student = new Student();
        student.setName("zhangsan");
        changeField(student);
        System.out.println(student);

        Teacher teacher = new Teacher();
        System.out.println(teacher);
        changeField(teacher);
        System.out.println(teacher);

    }

    //传递过来任意对象
    private void changeField(Object object) throws IllegalAccessException {
        //1.获得字节码对象
        Class clazz = object.getClass();
        //2.获得所有的属性Field fields[]
        Field[] fields = clazz.getDeclaredFields();
        //3.遍历所有的Field，如果是String类型，将包含的a改成b
        for (Field field : fields) {
            //判断是不是String类型
            if (field.getType() == String.class) {
                field.setAccessible(true);
                //student.getName(); 并不是调用getName
                //student.name
                String oldValue = (String) field.get(object);
                if (oldValue != null && !oldValue.equals("")) {
                    String newValue = oldValue.replace("a", "b");
                    //student.name = "lisi"
                    //student.setName("lisi")
                    field.set(object, newValue);
                }
            }
        }
    }




}
